/*
Author: Surpass-HC
Source: https://gitee.com/hxchuan000/clpc-1
Mail: surpass-hc@qq.com
*/
#include <lxPipCmd.h>

void cPipCmdMgrInit(cPipCmdMgrStc *pMgrPtr, cStrCmdInfStc *cfgCmdList, int cfgNbr, const char *cfgPipFile, void *usrData)
{
    if (!(pMgrPtr && cfgCmdList && (cfgNbr > 0) && cfgPipFile))
    {
        logerrorc("");
        return;
    }
    cStrCmdMgrInit(&pMgrPtr->cmdMgr, cfgCmdList, cfgNbr, pMgrPtr);
    pipeOptInit(&pMgrPtr->pipOpt, cfgPipFile, 50);
    pMgrPtr->usrData = usrData;
    return;
}

void cPipCmdMgrUp(cPipCmdMgrStc *pMgrPtr)
{
    char tmpStr[256];
    int tmplen;
    if (!(pMgrPtr))
    {
        logerrorc("");
        return;
    }
    tmplen = pipeOptRead(&pMgrPtr->pipOpt, tmpStr, sizeof(tmpStr));
    if (!(tmplen > 0 && tmplen < sizeof(tmpStr)))
    {
        // have no data
        return;
    }
    logdebugc("input str:%s", tmpStr);
    if (tmpStr[tmplen - 1] == '\n')
    {
        tmpStr[tmplen - 1] = '\0';
    }
    char *tmpCmdEndPtr = strchr(tmpStr, ' ');
    if(tmpCmdEndPtr != NULL)
    {
        tmpCmdEndPtr[0] = '\0';
        tmpCmdEndPtr++;
    }
    cStrCmdMgrDoCmd(&pMgrPtr->cmdMgr, tmpStr, tmpCmdEndPtr);
}
